package com.share.device.receiver.processSync;

import com.alibaba.fastjson.JSON;
import com.share.common.rabbit.model.CanalSyncMessage;
import com.share.system.api.domain.Region;
import com.share.system.api.service.RegionRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class ProcessRedisSync {

    @Autowired
    private RegionRedisService regionRedisService;


    /**
     * todo 删除操作，在获取数据时，如果数据为删除标识，则不展示
     */
    public boolean processRedisSync(CanalSyncMessage canalSyncMessage) {
        try {
            boolean b = false;
            // TODO: 实现具体的业务逻辑处理,写入redis的实现
            if ("INSERT".equals(canalSyncMessage.getType()) || "UPDATE".equals(canalSyncMessage.getType())) {
                /**
                 * 存在问题，Json数据转换问题
                 */
                for (Map<String, Object> data:canalSyncMessage.getData()
                ) {
                    Region region = JSON.parseObject(JSON.toJSONString(data), Region.class);
                    b = regionRedisService.saveRegion(region);
                    log.info("写入redis成功: {}", b);
                }
            }
            return b;
        } catch (Exception e) {
            log.error("Redis同步处理失败: {}", canalSyncMessage, e);
            throw new RuntimeException("Redis同步处理失败"+ e.getMessage());
        }
    }

}
